import axios from "axios";
import { MessageBox } from "element-ui";
import dataToken from "@/dataConstant/dataToken";
const service = axios.create({
  timeout: 5000
});
service.interceptors.request.use(
  config => {
    config.headers["token"] = dataToken.token;
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
);
// response interceptor
service.interceptors.response.use(
  response => {
    return handleReponseData(response);
  },
  error => {
    // Message({
    //   message: '登录连接超时（后台不能连接，请联系系统管理员）',
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    // alert(error);
    // window.location.href = "http://jajava.cn/products";
    return Promise.reject(error);
  }
);

/**
 * 返回数据逻辑处理
 * @param response
 * @returns {Promise<never>|*}
 */
function handleReponseData(response) {
  const res = response.data;
  if (res.errno === 501) {
    MessageBox.alert("系统未登录，请重新登录", "错误", {
      confirmButtonText: "确定",
      type: "error"
    }).then(() => {
      // 跳转到登录页
    });
    return Promise.reject(new Error("error"));
  } else {
    return response;
  }
}

export default service;
